package com.ruoyi.quartz.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.domain.WechatyInfo;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.quartz.service.IWechatyInfoService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 微信机器人信息Controller
 * 
 * @author fz
 * @date 2024-04-23
 */
@RestController
@RequestMapping("/wechaty/info")
public class WechatyInfoController extends BaseController
{
    @Autowired
    private IWechatyInfoService wechatyInfoService;
    @Autowired
    private ISysJobService sysJobService;

    /**
     * 查询微信机器人信息列表
     */
    @PreAuthorize("@ss.hasPermi('wechaty:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(WechatyInfo wechatyInfo)
    {
        startPage();
        List<WechatyInfo> list = wechatyInfoService.selectWechatyInfoList(wechatyInfo);
        return getDataTable(list);
    }

    /**
     * 导出微信机器人信息列表
     */
    @PreAuthorize("@ss.hasPermi('wechaty:info:export')")
    @Log(title = "微信机器人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WechatyInfo wechatyInfo)
    {
        List<WechatyInfo> list = wechatyInfoService.selectWechatyInfoList(wechatyInfo);
        ExcelUtil<WechatyInfo> util = new ExcelUtil<WechatyInfo>(WechatyInfo.class);
        util.exportExcel(response, list, "微信机器人信息数据");
    }

    /**
     * 获取微信机器人信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wechaty:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(wechatyInfoService.selectWechatyInfoById(id));
    }

    /**
     * 新增微信机器人信息
     */
    @PreAuthorize("@ss.hasPermi('wechaty:info:add')")
    @Log(title = "微信机器人信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WechatyInfo wechatyInfo)
    {
        return toAjax(wechatyInfoService.insertWechatyInfo(wechatyInfo));
    }

    /**
     * 修改微信机器人信息
     */
    @PreAuthorize("@ss.hasPermi('wechaty:info:edit')")
    @Log(title = "微信机器人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WechatyInfo wechatyInfo)
    {
        return toAjax(wechatyInfoService.updateWechatyInfo(wechatyInfo));
    }

    /**
     * 删除微信机器人信息
     */
    @PreAuthorize("@ss.hasPermi('wechaty:info:remove')")
    @Log(title = "微信机器人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(wechatyInfoService.deleteWechatyInfoByIds(ids));
    }

    @GetMapping("/restart")
    public AjaxResult restart() throws SchedulerException {
        SysJob restartJob=sysJobService.selectJobById(112L);
        sysJobService.run(restartJob);
        return success();
    }

    @GetMapping("/start")
    public AjaxResult start() throws SchedulerException {
        SysJob startJob=sysJobService.selectJobById(111L);
        sysJobService.run(startJob);
        return success();
    }
}
